<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="断面名称" prop="crossNm">
        <el-input
          v-model="queryParams.crossNm"
          placeholder="请输入断面名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="断面类型" prop="crossType">
        <el-select
          v-model="queryParams.crossType"
          placeholder="请选择断面类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.cross_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所在渠道" prop="chanCd">
        <el-input
          v-model="queryParams.chanCd"
          placeholder="请输入所在渠道"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="对应水闸" prop="sluCd">
        <el-select v-model="queryParams.sluCd" placeholder="请选择对应水闸" clearable >
                <el-option
                  v-for="dict in szdata"
                  :key="dict.sluCd"
                  :label="dict.sluNm"
                  :value="dict.sluCd"
                ></el-option>
              </el-select>
        <!-- <el-input
          v-model="queryParams.sluCd"
          placeholder="请输入对应水闸"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
      </el-form-item>
      <el-form-item label="管理机构" prop="orgCd">
        <el-input
          v-model="queryParams.orgCd"
          placeholder="请输入管理机构"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="行政区划" prop="addvCd">
        <el-input
          v-model="queryParams.addvCd"
          placeholder="请输入归属行政区划"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['datam:StCrossB:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['datam:StCrossB:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['datam:StCrossB:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['datam:StCrossB:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="StCrossBList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="断面名称"
        align="left"
        width="300"
        :show-overflow-tooltip="true"
        prop="crossNm"
      >
      </el-table-column>
      <!--<el-table-column label="断面经度" align="center" prop="crossE">-->
      <!--</el-table-column>-->
      <!--<el-table-column label="断面纬度" align="center" prop="crossN">-->
      <!--</el-table-column>-->
      <el-table-column label="断面类型" align="center" prop="crossType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.cross_type"
            :value="scope.row.crossType"
          />
        </template>
      </el-table-column>
      <el-table-column label="断面形状" align="center" prop="crossShape">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.cross_shape"
            :value="scope.row.crossShape"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="断面地址"
        :show-overflow-tooltip="true"
        align="left"
        prop="crossAddr"
      >
      </el-table-column>
      <!--<el-table-column label="渠口宽度" align="center" prop="topWid">-->
      <!--</el-table-column>-->
      <!--<el-table-column label="渠底宽度" align="center" prop="bedWid">-->
      <!--</el-table-column>-->
      <!--<el-table-column label="渠深" align="center" prop="crossH">-->
      <!--</el-table-column>-->
      <!--<el-table-column label="渠底高程" align="center" prop="bedDemo">-->
      <!--</el-table-column>-->
      <el-table-column
        label="所在渠道"
        :show-overflow-tooltip="true"
        align="left"
        prop="chanNm"
      >
      </el-table-column>
      <el-table-column label="对应水闸" :show-overflow-tooltip="true" align="left" prop="sluNm" >

      </el-table-column>
      <el-table-column
        label="管理机构"
        :show-overflow-tooltip="true"
        align="left"
        prop="orgNm"
      >
      </el-table-column>
      <el-table-column
        label="行政区划"
        :show-overflow-tooltip="true"
        align="center"
        prop="addvNm"
      >
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['datam:StCrossB:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['datam:StCrossB:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改渠道断面信息登记对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" class="dmxx" >
        <el-row>
          <el-col :span="12">
            <el-form-item label="断面名称" prop="crossNm">
              <el-input v-model="form.crossNm" placeholder="请输入断面名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="断面地址" prop="crossAddr">
              <el-input v-model="form.crossAddr" placeholder="请输入断面地址" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="断面经度" prop="crossE">
              <el-input v-model="form.crossE" placeholder="请输入断面经度" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="断面纬度" prop="crossN">
              <el-input v-model="form.crossN" placeholder="请输入断面纬度" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="断面类型" prop="crossType">
              <el-select v-model="form.crossType" placeholder="请选择断面类型">
                <el-option
                  v-for="dict in dict.type.cross_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="断面形状" prop="crossShape">
              <el-select v-model="form.crossShape" placeholder="请选择断面形状">
                <el-option
                  v-for="dict in dict.type.cross_shape"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="渠口宽度" prop="topWid">
              <el-input v-model="form.topWid" placeholder="请输入渠口宽度" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="渠底宽度" prop="bedWid">
              <el-input v-model="form.bedWid" placeholder="请输入渠底宽度" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="渠深" prop="crossH">
              <el-input v-model="form.crossH" placeholder="请输入渠深" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="渠底高程" prop="bedDemo">
              <el-input v-model="form.bedDemo" placeholder="请输入渠底高程" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="最高水位" prop="maxZ">
              <el-input v-model="form.maxZ" placeholder="请输入最高水位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最低水位" prop="minZ">
              <el-input v-model="form.minZ" placeholder="请输入最低水位" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="所在渠道" prop="chanCd">
              <treeselect
                v-model="form.chanCd"
                :options="qdOptions"
                :normalizer="qdnormalizer"
                :show-count="true"
                placeholder="所在渠道"
                @select="selectAd"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="对应水闸" prop="sluCd">
              <el-select v-model="form.sluCd" placeholder="请选择对应水闸" clearable >
                <el-option
                  v-for="dict in szdata"
                  :key="dict.sluCd"
                  :label="dict.sluNm"
                  :value="dict.sluCd"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="水管机构" prop="orgCd">
              <treeselect
                v-model="form.orgCd"
                :options="sgjgOptions"
                :normalizer="sgjgnormalizer"
                :show-count="true"
                placeholder="水管机构"
                @select="selectAd"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="行政区划" prop="addvCd">
              <treeselect
                v-model="form.addvCd"
                :options="xzqhOptions"
                :normalizer="xzqhnormalizer"
                :show-count="true"
                placeholder="行政区划"
                @select="selectAd"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="设置日期" prop="crossDate">
              <el-date-picker
                clearable
                v-model="form.crossDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择设置日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="crossMark">
              <el-input v-model="form.crossMark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listStCrossB,
  getStCrossB,
  delStCrossB,
  addStCrossB,
  updateStCrossB,
} from "@/api/datam/StCrossB";
import {allListWcSluiceB} from "@/api/datam/WcSluiceB"
import { organtreeList,CmAddvBList,WcChanBList } from "@/api/common";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "StCrossB",
  components: { Treeselect },
  dicts: ["cross_type", "cross_shape"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 渠道断面信息登记表格数据
      StCrossBList: [],
      sgjgOptions: [],
      xzqhOptions: [],
      qdOptions:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        crossNm: null,
        crossAddr: null,
        crossE: null,
        crossN: null,
        crossType: null,
        crossShape: null,
        topWid: null,
        bedWid: null,
        crossH: null,
        bedDemo: null,
        maxZ: null,
        minZ: null,
        chanCd: null,
        sluCd: null,
        orgCd: null,
        addvCd: null,
        crossDate: null,
        crossMark: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      szdata:[]
    };
  },
  created() {
    this.getTreeselect();
    this.getCmAddvBTreeselect();
    this.getWcChanBTreeselect();
    this.getszList();
    this.getList();
  },
  methods: {
    /** 查询对应水闸 */
    getszList() {
      allListWcSluiceB().then((response) => {
        let sd = response.rows;
        this.szdata = sd;
      });
    },
    /** 查询水管机构下拉树结构 */
    getTreeselect() {
      organtreeList().then((response) => {
        this.sgjgOptions = this.handleTree(
          response.data,
          "orgCd",
          "parentOrgCd"
        );
      });
    },
    /** 查询行政区域下拉树结构 */
    getCmAddvBTreeselect() {
      CmAddvBList().then((response) => {
        this.xzqhOptions = this.handleTree(
          response.data,
          "addvCd",
          "parentAddvCd"
        );
      });
    },
    /** 查询渠系下拉树结构 */
    getWcChanBTreeselect() {
      WcChanBList().then((response) => {
        this.qdOptions = this.handleTree(
          response.data,
          "chanCd",
          "parentChanCd"
        );
      });
    },
    /** 转换水管机构菜单数据结构 */
    sgjgnormalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.orgCd,
        label: node.orgNm,
        children: node.children,
      };
    },
    /** 转换行政区划菜单数据结构 */
    xzqhnormalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.addvCd,
        label: node.addvNm,
        children: node.children,
      };
    },
    /** 转换渠道菜单数据结构 */
    qdnormalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.chanCd,
        label: node.chanNm,
        children: node.children,
      };
    },

    // 获取区划名称
    selectAd(val) {
      // this.queryParams.tableTitle = val.orgNm
      // this.getlistWiFarmer()
    },
    /** 查询渠道断面信息登记列表 */
    getList() {
      this.loading = true;
      listStCrossB(this.queryParams).then((response) => {
        this.StCrossBList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        crossCd: null,
        crossNm: null,
        crossAddr: null,
        crossE: null,
        crossN: null,
        crossType: null,
        crossShape: null,
        topWid: null,
        bedWid: null,
        crossH: null,
        bedDemo: null,
        maxZ: null,
        minZ: null,
        chanCd: null,
        sluCd: null,
        orgCd: null,
        addvCd: null,
        crossDate: null,
        crossMark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.crossCd);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加渠道断面信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const crossCd = row.crossCd || this.ids;
      getStCrossB(crossCd).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改渠道断面信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.crossCd != null) {
            updateStCrossB(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStCrossB(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const crossCds = row.crossCd || this.ids;
      this.$modal
        .confirm(
          '是否确认删除渠道断面信息登记编号为"' + crossCds + '"的数据项？'
        )
        .then(function () {
          return delStCrossB(crossCds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "datam/StCrossB/export",
        {
          ...this.queryParams,
        },
        `断面信息_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-table .cell {
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  word-break: break-all;
  line-height: 23px;
  padding-left: 10px;
  padding-right: 10px;
}

.dmxx{
  .el-select {
    width: 100%;
  }

  .vue-treeselect{
      width: 100%;
  }

  .el-date-editor{
      width: 100%;
  }
}


</style>
